package com.qiji.video.service.async;

import com.qiji.video.bean.Users;
import com.qiji.video.mapper.extend.UserExtendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Component
public class AsyncService {

    @Autowired
    private UserExtendMapper userExtendMapper;
    @Autowired
    @Qualifier("RedisTemplate")
    private RedisTemplate redisTemplate;

    private HashMap map = new HashMap();

    @Async
    public void updateLoginTime(Users user,String username){
        if (username.indexOf("@")>=0){
            map.put("email",username);
        }else{
            map.put("phone",username);
        }
        // 更新登录时间
        map.put("loginTime",new Timestamp(System.currentTimeMillis()));
        Integer result = userExtendMapper.updateLoginTimeByPhone(map);
        if (result>=0){
            // 更新缓存(Redis)
            user.setLoginTime(new Timestamp(System.currentTimeMillis()));
            redisTemplate.opsForHash().put("Login:User:"+username+"",username,user);
            // 过期时间
            redisTemplate.expire("Login:User:"+username+"", 2,TimeUnit.DAYS);
        }
    }
}
